limsQualitycontrolplan.editPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-qualitycontrolplan-edit-list-grid',
            url: limsQualitycontrolplan.baseUrl + '/queries/raw',
            service: 'limsQualitycontrolplanServiceImpl',
            dbTable: 'T_LIMS_QUALITYCONTROLPLAN',
            columns: limsQualitycontrolplan.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'id' && _this.loadDetailPage(row.id);
            },
            deleteFormatter: function (row) {
                return row.mainBatchCode;
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.create('modal', {
                        width: 400,
                        height: 430,
                        title: 'BASELINE.BASEDATA.QUALITYCONTROLPLANS.MODAL.ADD',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            Gikam.getComp('lims-qualitycontrolplan-edit-list-grid').refresh();
                        },
                        items: [{
                            type: 'btnToolbar',
                            items: [{
                                type: 'button',
                                text: 'GIKAM.BUTTON.CONFIRM',
                                icon: 'save',
                                onClick: function () {
                                    if (Gikam.getComp('lims-qualitycontrolplan-base-info-form').validate()) {
                                        Gikam.postText(limsQualitycontrolplan.baseUrl, Gikam.getJsonWrapper(null, ['limsQualitycontrolplanServiceImpl', [Gikam.getComp('lims-qualitycontrolplan-base-info-form').getData()]]))
                                            .done(function () {
                                                Gikam.getComp('lims-qualitycontrolplan-edit-list-grid').refresh();
                                            });
                                        Gikam.getLastModal().close();
                                    }
                                }
                            }, {
                                type: 'button',
                                text: 'GIKAM.BUTTON.CANCEL',
                                icon: 'cancel',
                                onClick: function () {
                                    Gikam.getLastModal().close();
                                }
                            }]
                        }, {
                            type: 'form', // url : _this.getCompUrl('baseInfoForm'),
                            service: 'limsQualitycontrolplanServiceImpl',
                            dbTable: 'T_LIMS_QUALITYCONTROLPLAN',
                            id: 'lims-qualitycontrolplan-base-info-form',
                            fields: limsQualitycontrolplan.getBaseInfoFormFields(),
                            columns: 1,
                            titleWidth: 120,
                            onRendered: function () {
                                var groupSelectUrl = limsQualitycontrolplan.baseGroupUrl + '/get-groupselectitems'
                                Gikam.postText(groupSelectUrl, Gikam.getJsonWrapper(null, [null, null])).done(function (req) {
                                    if (req) {
                                        req = JSON.parse(req)
                                        Gikam.getComp('lims-qualitycontrolplan-base-info-form').setSelectOptions('professionalGroup', req.items)
                                    }
                                })
                            },
                        },]
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    _this.checkIfStillInUseAndDelete('planGrid')
                }
            }, {
                type: 'button',
                text: 'T_LIMS_QUALITYCONTROLPLAN.ACTIVESTATUS.OPTIONS.OFF',
                icon: 'disable',
                onClick: function () {
                    var grid_ = Gikam.getComp('lims-qualitycontrolplan-edit-list-grid')
                    var setectRows = grid_.getSelections()
                    if (setectRows.length > 0) {
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.TIPS.OFF', function () {
                            var ids = setectRows.map(function (item) {
                                return {id: item.id}
                            })
                            Gikam.postText(limsQualitycontrolplan.baseUrl + '/activeoff', Gikam.getJsonWrapper(null, ['limsQualitycontrolplanServiceImpl', ids])).done(function (data) {
                                _this.refreshGrids();
                            });
                        })
                    } else {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                    }
                }
            }],
            requestData: {
                activeStatus_EQ: 'on'
            },
            activateFirstRow: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getAsyncComp('lims-qualitycontrolplan-project-edit-list-grid').done(function (grid) {
                        grid.cleanData();
                    })
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-qualitycontrolplan-project-edit-list-grid').done(function (grid) {
                    grid.refresh({
                        url: limsQualitycontrolplan.baseProjectUrl + '/queries/raw', requestData: {
                            planId_EQ: row.id
                        }
                    });
                })
            }
        }
    },

    getOffGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-qualitycontrolplan-edit-list-grid-off', // url: limsQualitycontrolplan.baseUrl + '/queries/raw',
            service: 'limsQualitycontrolplanServiceImpl',
            dbTable: 'T_LIMS_QUALITYCONTROLPLAN',
            columns: limsQualitycontrolplan.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'id' && _this.loadDetailPage(row.id);
            },
            deleteFormatter: function (row) {
                return row.mainBatchCode;
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'T_LIMS_QUALITYCONTROLPLAN.ACTIVESTATUS.OPTIONS.ON',
                icon: 'enable',
                onClick: function () {
                    var grid_ = Gikam.getComp('lims-qualitycontrolplan-edit-list-grid-off')
                    var setectRows = grid_.getSelections()
                    if (setectRows.length > 0) {
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.TIPS.ON', function () {
                            var ids = setectRows.map(function (item) {
                                return {id: item.id}
                            })
                            Gikam.postText(limsQualitycontrolplan.baseUrl + '/activeon', Gikam.getJsonWrapper(null, ['limsQualitycontrolplanServiceImpl', ids])).done(function (data) {
                                _this.refreshGrids()
                            })
                        })
                    } else {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                    }
                }
            }],
            requestData: {
                activeStatus_EQ: 'off'
            },
            activateFirstRow: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getAsyncComp('lims-qualitycontrolplan-project-edit-list-grid').done(function (grid) {
                        grid.cleanData();
                    })
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-qualitycontrolplan-project-edit-list-grid').done(function (grid) {
                    grid.refresh({
                        url: limsQualitycontrolplan.baseProjectUrl + '/queries/raw', requestData: {
                            planId_EQ: row.id
                        }
                    });
                })
            }
        }
    },

    checkIfStillInUseAndDelete: function (gridType) {
        var _this = this
        if (gridType) {
            var checkUrl = limsQualitycontrolplan.baseUrl + '/check-ifstill-inuse'
            var data = {
                gridType: gridType
            }
            if (gridType == 'planGrid') {
                var grid = Gikam.getComp('lims-qualitycontrolplan-edit-list-grid')
                var setectRows = grid.getSelections()
                Gikam.postText(checkUrl, Gikam.getJsonWrapper(data, [null, setectRows])).done(function (req) {
                    req = JSON.parse(req)
                    if (req.flag) {
                        Gikam.alert(Gikam.propI18N('QUALITY_CONTROL.PLANPROJECT.CANNOT_DELETE') + '<br/>' + req.message)
                    } else {
                        Gikam.getComp('lims-qualitycontrolplan-edit-list-grid').deleteRows(limsQualitycontrolplan.baseUrl)
                    }
                })
            } else if (gridType == 'projectGrid') {
                var grid = Gikam.getComp('lims-qualitycontrolplan-project-edit-list-grid')
                var setectRows = grid.getSelections()
                Gikam.postText(checkUrl, Gikam.getJsonWrapper(data, [null, setectRows])).done(function (req) {
                    req = JSON.parse(req)
                    if (req.flag) {
                        Gikam.alert(Gikam.propI18N('QUALITY_CONTROL.PLANPROJECT.CANNOT_DELETE') + '<br/>' + req.message)
                    } else {
                        Gikam.getComp('lims-qualitycontrolplan-project-edit-list-grid').deleteRows(limsQualitycontrolplan.baseProjectUrl)
                    }
                })
            }
        }
    },

    refreshGrids() {
        Gikam.getComp('lims-qualitycontrolplan-edit-list-grid').refresh()
        Gikam.getComp('lims-qualitycontrolplan-edit-list-grid-off').refresh()
    },

    loadDetailPage: function (id) {
        workspace.window.load(limsQualitycontrolplan.baseUrl + '/page/detail' + Gikam.param({
            id: id
        }));
    },

    getProjectGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-qualitycontrolplan-project-edit-list-grid', // url: limsQualitycontrolplan.baseProjectUrl + '/queries',
            service: 'limsQualitycontrolplanProjectServiceImpl',
            dbTable: 'T_LIMS_QUALITYCONTROLPLAN_PROJECT',
            columns: limsQualitycontrolplan.getProjectEditListColumns(),
            onCellClick: function (field, row) {
                field === 'id' && _this.loadDetailPage(row.id);
            },
            deleteFormatter: function (row) {
                return row.productName
            },
            page: true,
            toolbar: [{
                type: 'button', text: 'GIKAM.BUTTON.INSERT', icon: 'add', onClick: function () {
                    Gikam.getAsyncComp('lims-qualitycontrolplan-edit-list-grid').done(function (planGrid) {
                        if (planGrid) {
                            Gikam.create('modal', {
                                width: 400,
                                height: 370,
                                title: 'BASELINE.BASEDATA.QUALITYCONTROLPLANS.MODAL.ADD',
                                onAfterClose: function (data) {
                                    if (Gikam.isEmpty(data)) {
                                        return;
                                    }
                                    Gikam.getComp('lims-qualitycontrolplan-project-edit-list-grid').refresh();
                                },
                                items: [{
                                    type: 'btnToolbar', items: [{
                                        type: 'button',
                                        text: 'GIKAM.BUTTON.CONFIRM',
                                        icon: 'save',
                                        onClick: function () {
                                            if (Gikam.getComp('lims-qualitycontrolplanproject-base-info-form').validate()) {
                                                var saveData = Gikam.getComp('lims-qualitycontrolplanproject-base-info-form').getData()
                                                saveData.planId = planGrid.getActivedRow().id
                                                Gikam.postText(limsQualitycontrolplan.baseProjectUrl, Gikam.getJsonWrapper(null, ['limsQualitycontrolplanProjectServiceImpl', [saveData]]))
                                                    .done(function () {
                                                        Gikam.getComp('lims-qualitycontrolplan-project-edit-list-grid').refresh();
                                                    });
                                                Gikam.getLastModal().close();
                                            }
                                        }
                                    }, {
                                        type: 'button',
                                        text: 'GIKAM.BUTTON.CANCEL',
                                        icon: 'cancel',
                                        onClick: function () {
                                            Gikam.getLastModal().close();
                                        }
                                    }]
                                }, {
                                    type: 'form',
                                    service: 'limsQualitycontrolplanProjectServiceImpl',
                                    id: 'lims-qualitycontrolplanproject-base-info-form',
                                    fields: limsQualitycontrolplan.getProjectBaseInfoFormFields(),
                                    onRendered: function () {
                                        var controlRuleSelectUrl = limsQualitycontrolplan.baseProjectUrl + '/controlrule-select'
                                        Gikam.postText(controlRuleSelectUrl, Gikam.getJsonWrapper(null, [null, null])).done(function (req) {
                                            if (req) {
                                                req = JSON.parse(req)
                                                Gikam.getComp('lims-qualitycontrolplanproject-base-info-form').setSelectOptions('controlRule', req.items)
                                            }
                                        })
                                    },
                                    columns: 1,
                                }]
                            });
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    _this.checkIfStillInUseAndDelete('projectGrid')
                }
            }],
        }
    },

    getMainTab: function () {
        var _this = this
        var first = false
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'T_LIMS_QUALITYCONTROLPLAN.ACTIVESTATUS.ON',
                items: [_this.getGridParam()]
            }, {
                title: 'T_LIMS_QUALITYCONTROLPLAN.ACTIVESTATUS.OFF',
                items: [_this.getOffGridParam()]
            }],
            onActived: function (data) {
                var tab = Gikam.getComp('main-tab');
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp('lims-qualitycontrolplan-edit-list-grid').done(function (grid) {
                                grid.refresh()
                            })
                            Gikam.getAsyncComp('lims-qualitycontrolplan-project-edit-list-grid').done(function (grid) {
                                grid.showToolbar()
                            })
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp('lims-qualitycontrolplan-edit-list-grid-off').done(function (grid) {
                            grid.refresh({
                                url: limsQualitycontrolplan.baseUrl + '/queries/raw',
                            })
                        })
                        Gikam.getAsyncComp('lims-qualitycontrolplan-project-edit-list-grid').done(function (grid) {
                            grid.hideToolbar()
                        })
                        break
                }
                first = true;
            },
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-qualitycontrolplan-edit-list-layout',
            renderTo:
            workspace.window.$dom,
            north: {
                items: [this.getMainTab()],
                height: '50%'
            }, center: {
                items: [this.getProjectGridParam()],
            }
        });
    },

    init: function () {
        this.create();
    }
};
